﻿package com.designoutput.ui.newControls
{
    import com.designoutput.event.SliderEvent;
    import com.designoutput.icon.SlidebarDragOut;
    import com.designoutput.ui.controls.IconButton;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;

    public class Slider extends Sprite
    {
        private var _size:int = 100;
        private var _drag:Sprite;
		private var _minValue:Number;
		private var _maxValue:Number;
		private var _value:Number;

        public function Slider(size:int, defaultValue:Number = 100, minValue:Number = 0, maxValue:Number = 1)
        {
            _size = size;
			_value = defaultValue;
			_minValue = minValue;
			_maxValue = maxValue;
			
			graphics.beginFill(14935011);
			graphics.drawRect(1, 10, (size - 1), 4);
			graphics.beginFill(9079434);
			graphics.drawRect(0, 10, (size - 1), 3);
			graphics.beginFill(13882323);
			graphics.drawRect(1, 11, size - 2, 2);
			graphics.endFill();
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
			
            _drag = new IconButton(new SlidebarDragOut(0, 0));			
            _drag.y = 3;
            _drag.addEventListener(MouseEvent.MOUSE_DOWN, drag_mouseDownHandler, false, 0, true);
            addChild(_drag);
			
			updateDisplay();
        }
		
		public function getValue():Number
		{
			return _value;
		}
		
		public function setValue(value:Number) : void
		{
			_value = value;
			if(_value > _maxValue)
				_value = _maxValue;
			else if(_value < _minValue)
				_value = _minValue;
			updateDisplay();
		}
		
		private function updateDisplay():void {
			var ratio:Number = (_value - _minValue) / (_maxValue - _minValue);
			_drag.x = Math.round(ratio * _size - 4); //4 is half width of drag, to align it
		}

		private function updateValueFromDrag():void {
			_value = ((_drag.x + 4) / _size) * (_maxValue - _minValue) + _minValue;
		}

        private function mouseDownHandler(event:MouseEvent) : void
        {
            if (mouseX > 0 && mouseX < _size)
            {
                _drag.x = Math.round(mouseX - 4);
                updateValueFromDrag();
            }
        }

		private function drag_mouseDownHandler(event:MouseEvent) : void
        {
            _drag.startDrag(false, new Rectangle(-4, 3, _size, 0));
            stage.addEventListener(MouseEvent.MOUSE_MOVE, drag_mouseMoveHandler, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, drag_stageMouseUpHandler, false, 0, true);
        }
		
		private function drag_mouseMoveHandler(event:Event) : void
		{
			updateValueFromDrag();
			dispatchEvent(new SliderEvent(SliderEvent.DRAG, _value, event == null));
		}
		
		private function drag_stageMouseUpHandler(event:MouseEvent) : void
		{
			_drag.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag_mouseMoveHandler, false);
			stage.removeEventListener(MouseEvent.MOUSE_UP, drag_stageMouseUpHandler, false);
			dispatchEvent(new SliderEvent(SliderEvent.DRAG, _value, event == null));
			dispatchEvent(new SliderEvent(SliderEvent.RELEASE, _value));
		}
    }
}